package adapter.order_adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.hutoubenjg.R;

import java.text.DecimalFormat;
import java.util.List;

import activity.order.OrderDeilActivity;
import bean.order_bean.WeixiuKeJieBen;
import util.utls.BiaduMaps;
import util.utls.Const;
import util.utls.OrderBtnSed;
import util.utls.XImageLoader;


/**
 * 可接订单
 */

public class OrdersAdapter extends BaseAdapter implements View.OnClickListener {
    Context Context;
    OrderBtnSed send;
    double lng;
    double lat;
    List<WeixiuKeJieBen.InfoBean> list;

    public OrdersAdapter(Context Context, List<WeixiuKeJieBen.InfoBean> list, double lng, double lat, OrderBtnSed send) {
        this.Context = Context;
        this.list = list;
        this.lng = lng;
        this.lat = lat;
        this.send = send;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHouder houder;
        if (convertView == null) {
            houder = new ViewHouder();
            convertView = LayoutInflater.from(Context).inflate(R.layout.item_orders, null);
            houder.tvNumSize = (TextView) convertView.findViewById(R.id.tvmmSizes);
            houder.tvMatter = (TextView) convertView.findViewById(R.id.tvMatter);
            houder.tvaddress = (TextView) convertView.findViewById(R.id.tvaddress);
            houder.btnSend = (Button) convertView.findViewById(R.id.btnSend);
//            houder.img1 = (ImageView) convertView.findViewById(R.id.img1);
//            houder.img2 = (ImageView) convertView.findViewById(R.id.img2);
//            houder.img3 = (ImageView) convertView.findViewById(R.id.img3);
            convertView.setTag(houder);
        } else {
            houder = (ViewHouder) convertView.getTag();
        }
        double lngt = list.get(position).getLng();
        double lnat = list.get(position).getLat();
        Log.d("latlaltl",lngt+""+lnat);
        double distance = BiaduMaps.GPSLng(lngt, lnat, lng, lat);
        double naece = distance / 1000;
        double b = Double.valueOf(naece);
        DecimalFormat df = new DecimalFormat("######0.00");
        houder.tvNumSize.setText(df.format(b) + "km(仅供参考)");

        houder.tvaddress.setText(list.get(position).getAddress());
        houder.tvMatter.setText(list.get(position).getVehicle_name());
        houder.btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Context, OrderDeilActivity.class);
                intent.putExtra("addres", list.get(position).getAddress());
                intent.putExtra("repairName", list.get(position).getVehicle_name());
                intent.putExtra("detail", list.get(position).getDetail());
                intent.putExtra("lng", list.get(position).getLng() + "");
                intent.putExtra("lat", list.get(position).getLat() + "");
                intent.putExtra("data", list.get(position).getCreate_time());
                intent.putExtra("phone", list.get(position).getMobile());
                intent.putExtra("id", list.get(position).getId());
                intent.putExtra("img", list.get(position).getImages());
                Log.d("putExtra", list.get(position).getLng() + "");
                Context.startActivity(intent);
            }
        });
        return convertView;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnSend:
                send.onBtnSend(v);
                break;
        }
    }

    public class ViewHouder {
        TextView tvNumSize, tvMatter, tvaddress;
        Button btnSend;
        ImageView img1, img2, img3;

    }

    class NoScrollGridAdapters extends BaseAdapter {

        /**
         * 上下文
         */
        private Context ctx;
        /**
         * 图片Url集合
         */
        WeixiuKeJieBen bean;

        public NoScrollGridAdapters(Context ctx, WeixiuKeJieBen bean) {
            this.ctx = ctx;
            this.bean = bean;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return bean.getList().size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(ctx, R.layout.item_gridview, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.iv_image);

            Log.d("oaoaoaoaoaoa", bean.getList().get(position).getImages());
            if (bean.getList().get(position).getImages() == null || bean.getList().get(position).getImages() == "") {
                XImageLoader.load(Const.IMAGE_HEAD, imageView);
            } else {
                XImageLoader.load(Const.IMAGE_HEAD + bean.getList().get(position).getImages(), imageView);
            }
            return view;
        }
    }
}
